global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/figure_A4_lags_and_leads.log, replace name(fig)

capture noi {

	clear
	estimates clear
	set more off
	*The horserace checks different lag and lead times
	*******************
	* Panel A -- CYFE
	*******************
	use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
	tab year, gen(YY_)
	*the usual setting up for more convenient naming inside the file
	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0 
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0	
	clonevar LSW = lswMPm_1995_a
	clonevar HSW = hswMPm_1995_a
	clonevar VAEMP = vaempMPm_1995_a
	clonevar GDPGAP = lngdpgap_1995_a	
	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
	sort lse_id year
	*generate dummies for FE
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearindustry = group(year industry) if year <= 2009

	* 1 -5
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+5 & year <= 2009+5
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe F5.${depvar}_${ttt} LSW HSW GDPGAP VAEMP F3.stockown F3.stockownzero F3.stockother F3.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_1 = e(b)
	mat vmat_1_1 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_1
	drop total_${depvar}_${ttt}_1995	
	* 2 -4
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+4 & year <= 2009+4
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe F4.${depvar}_${ttt} LSW HSW GDPGAP VAEMP F2.stockown F2.stockownzero F2.stockother F2.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_2 = e(b)
	mat vmat_1_2 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_2
	drop total_${depvar}_${ttt}_1995	
	* 3 -3
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+3 & year <= 2009+3
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe F3.${depvar}_${ttt} LSW HSW GDPGAP VAEMP F1.stockown F1.stockownzero F1.stockother F1.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_3 = e(b)
	mat vmat_1_3 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_3
	drop total_${depvar}_${ttt}_1995	
	* 4 -2
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_4 = e(b)
	mat vmat_1_4 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_4
	drop total_${depvar}_${ttt}_1995	
	* 5 -1
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+1 & year <= 2009+1
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe F1.${depvar}_${ttt} LSW HSW GDPGAP VAEMP L1.stockown L1.stockownzero L1.stockother L1.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_5 = e(b)
	mat vmat_1_5 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_5
	drop total_${depvar}_${ttt}_1995
	* 6 0
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995 & year <= 2009
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe ${depvar}_${ttt} LSW HSW GDPGAP VAEMP L2.stockown L2.stockownzero L2.stockother L2.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_6 = e(b)
	mat vmat_1_6 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_6
	drop total_${depvar}_${ttt}_1995
	* 7 1
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995-1 & year <= 2009-1
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe L1.${depvar}_${ttt} LSW HSW GDPGAP VAEMP L3.stockown L3.stockownzero L3.stockother L3.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_7 = e(b)
	mat vmat_1_7 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_7
	drop total_${depvar}_${ttt}_1995
	* 8 2
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995-2 & year <= 2009-2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe L2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP L4.stockown L4.stockownzero L4.stockother L4.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_8 = e(b)
	mat vmat_1_8 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_8
	drop total_${depvar}_${ttt}_1995

	* 9 3
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995-3 & year <= 2009-3
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe L3.${depvar}_${ttt} LSW HSW GDPGAP VAEMP L5.stockown L5.stockownzero L5.stockother L5.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_9 = e(b)
	mat vmat_1_9 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_9
	drop total_${depvar}_${ttt}_1995

	* 10 4
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995-4 & year <= 2009-4
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe L4.${depvar}_${ttt} LSW HSW GDPGAP VAEMP L6.stockown L6.stockownzero L6.stockother L6.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_10 = e(b)
	mat vmat_1_10 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_10
	drop total_${depvar}_${ttt}_1995

	* 11 5
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995-5 & year <= 2009-5
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe L5.${depvar}_${ttt} LSW HSW GDPGAP VAEMP L7.stockown L7.stockownzero L7.stockother L7.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_1_11 = e(b)
	mat vmat_1_11 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p1_11
	drop total_${depvar}_${ttt}_1995
	drop stockown stockownzero stockother stockotherzero
	drop LSW HSW VAEMP GDPGAP
	drop spilloversown spilloversownzero spilloversother spilloversotherzero


	*********************************
	* Panel B -- CYFE FOREIGN + HOME
	*********************************

	use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
	tab year, gen(YY_)

	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0 
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0	
	foreach shr in shr4 {
		clonevar VAEMP_foreign = vaempMPm_`shr'_foreign_1995_a
		clonevar LSW_foreign = lswMPm_`shr'_foreign_1995_a
		clonevar HSW_foreign = hswMPm_`shr'_foreign_1995_a
		clonevar GDPGAP_foreign = lngdpgap_shr_foreign_1995_a	
	}
	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
	sort lse_id year
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearindustry = group(year industry) if year <= 2009

	* 1 -5
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+5 & year <= 2009+5
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe F5.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign F3.stockown F3.stockownzero F3.stockother F3.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_1 = e(b)
	mat vmat_2_1 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_1
	drop total_${depvar}_${ttt}_1995	
	* 2 -4
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+4 & year <= 2009+4
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe F4.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign F2.stockown F2.stockownzero F2.stockother F2.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_2 = e(b)
	mat vmat_2_2 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_2
	drop total_${depvar}_${ttt}_1995	
	* 3 -3
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+3 & year <= 2009+3
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe F3.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign F1.stockown F1.stockownzero F1.stockother F1.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_3 = e(b)
	mat vmat_2_3 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_3
	drop total_${depvar}_${ttt}_1995	
	* 4 -2
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_4 = e(b)
	mat vmat_2_4 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_4
	drop total_${depvar}_${ttt}_1995	
	* 5 -1
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+1 & year <= 2009+1
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe F1.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign L1.stockown L1.stockownzero L1.stockother L1.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_5 = e(b)
	mat vmat_2_5 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_5
	drop total_${depvar}_${ttt}_1995
	* 6 0
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995 & year <= 2009
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe ${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign L2.stockown L2.stockownzero L2.stockother L2.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_6 = e(b)
	mat vmat_2_6 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_6
	drop total_${depvar}_${ttt}_1995
	* 7 1
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995-1 & year <= 2009-1
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe L1.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign L3.stockown L3.stockownzero L3.stockother L3.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_7 = e(b)
	mat vmat_2_7 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_7
	drop total_${depvar}_${ttt}_1995

	* 8 2
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995-2 & year <= 2009-2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe L2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign L4.stockown L4.stockownzero L4.stockother L4.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_8 = e(b)
	mat vmat_2_8 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_8
	drop total_${depvar}_${ttt}_1995

	* 9 3
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995-3 & year <= 2009-3
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe L3.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign L5.stockown L5.stockownzero L5.stockother L5.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_9 = e(b)
	mat vmat_2_9 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_9
	drop total_${depvar}_${ttt}_1995

	* 10 4
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995-4 & year <= 2009-4
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe L4.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign L6.stockown L6.stockownzero L6.stockother L6.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_10 = e(b)
	mat vmat_2_10 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_10
	drop total_${depvar}_${ttt}_1995

	* 11 5
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995-5 & year <= 2009-5
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995

	ppmlhdfe L5.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign L7.stockown L7.stockownzero L7.stockother L7.stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero  if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0,vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local has_stockspill "\yes"
	estadd local has_GDPGAP "\yes"
	mat cmat_2_11 = e(b)
	mat vmat_2_11 = e(V)
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store p2_11
	drop total_${depvar}_${ttt}_1995

	*************************
	* Output figure			*
	*************************


	* store the coefficients and confidence intervals (for each var x panel) in a matrix
	forvalues j = 1(1)2 {
		forvalues i = 1(1)2 {
			mat p`j'_`i' = J(3,11,.)
			mat colnames p`j'_`i' = -5 -4 -3 -2 -1 0 1 2 3 4 5
			mat rownames p`j'_`i' = coef_`i' lc95_`i' uc95_`i'
			forvalues z = 1(1)11 {
				mat p`j'_`i'[1,`z'] = cmat_`j'_`z'[1,`i']
				mat p`j'_`i'[2,`z'] = cmat_`j'_`z'[1,`i'] + invnormal(0.975)*sqrt(vmat_`j'_`z'[`i',`i'])
				mat p`j'_`i'[3,`z'] = cmat_`j'_`z'[1,`i'] - invnormal(0.975)*sqrt(vmat_`j'_`z'[`i',`i'])
			}
		}
	}

	qui include ${code_dir}/config/figuretools.do
	colorpalette ptol, n(2) nograph
	coefplot (matrix(p1_1), ci((2 3)) recast(connected) msymb(circle) mcolor("68 119 170") lcolor("68 119 170") ciopts(lwidth(none) recast(rarea) color("68 119 170%20")) label(Low-skill wage)) /// 
		(matrix(p1_2), ci((2 3)) recast(connected) msymb(S) mcolor("204 102 119") lcolor("204 102 119") ciopts(lwidth(none) recast(rarea) color("204 102 119%20")) label(High-skill wage)), bylabel("(A)   {it:Domestic and foreign}") /// 
		|| (matrix(p2_1), ci((2 3)) label(Low-skill wage)) /// 
		(matrix(p2_2), ci((2 3)) label(High-skill wage)), bylabel("(B)   {it:Foreign}") /// 
		||, vertical subtitle(,bcolor(none)) ciopts(pstyle(p1 p2 p3 p4)) nooffsets ///
		yline(0,lcolor(gs8)) ytitle("Coefficient") ylab(-6 -3 0 3 6) yscale(r(-8.5 8.5)) ///
		xtitle("Lags and leads (years)") xlab(2 "-4" 4 "-2" 6 "0" 8 "2" 10 "4") legend(rows(1))
	graph export ${fig_dir}/appendix/Figure_A4_lags_and_leads.pdf, as(pdf) replace
	graph export ${fig_dir}/appendix/Figure_A4_lags_and_leads.eps, as(eps) replace

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close fig
